package openadmin.jaas;

import java.io.IOException;

import javax.security.auth.callback.*;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

public class CallbackHandlerAyto implements CallbackHandler {

	private String usuari;
	private char[] credencials;
	
	public CallbackHandlerAyto( String pUsuari, char[] pCredencials) {
	      super();	      
	      this.usuari = pUsuari; 
	      this.credencials = pCredencials;	     
	   }
	
	@Override
	public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
						
		 for (int i = 0; i < callbacks.length; i++) {
	         if (callbacks[i] instanceof NameCallback) {
	            ((NameCallback) callbacks[i]).setName(usuari); 	            
	         }
	         else if (callbacks[i] instanceof PasswordCallback) {
	            ((PasswordCallback) callbacks[i]).setPassword(credencials);
	         } else {
	            throw new UnsupportedCallbackException(callbacks[i]);
	         }
	    }				
	}
}
